<?php

	class UserController extends Controller {

    function __construct(){
        if (isset($_SESSION['userID'])) {
            $login = new Login();
            if (!$login->get($_SESSION['userID']))
                header("Location: ".SITE_URL.'?c=Login');
        }
        else {
            header("Location: ".SITE_URL.'?c=Login');
        }
        
        parent::__construct();
    }

    function index(){

        $included_files = get_included_files();

        if (!in_array(APPLICATION_PATH.'/helpers/url_helper.php', $included_files))
                include APPLICATION_PATH.'/helpers/url_helper.php';
                
        $section = "User";
        $title = _LB_USER;
        $cfer = new Cfer(array(
        $title => base_url('?c=User')));
        
        $filter = array();
        $filter['name'] = isset($_POST['name']) ? $_POST['name'] : '';
        $filter['type'] = isset($_POST['type']) ? $_POST['type'] : '';
        $user = Login::getList($filter);

        $this->vars['user'] = $user;
        $this->vars['filter'] = $filter;
        $this->vars['title'] = $title;
        

        $array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);

        $this->vars['array_menus'] = $array_menus;
        $this->vars['section'] = $section;
        
        $this->display('user_list');
    }
    
    function change_password(){
        
        $included_files = get_included_files();

        if (!in_array(APPLICATION_PATH.'/helpers/url_helper.php', $included_files))
                include APPLICATION_PATH.'/helpers/url_helper.php';
        
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;

        $section = 'user';
		
        $title = _LB_CHANGE_PASSWORD;
        $cfer = new Cfer(array(
            _LB_USER => 'user',
            $title => base_url('?c=User&a=change_password&id='.$id)));
        $act = isset($_POST['act']) ? $_POST['act'] : false;
    
        $user = new Login();

        if (!$id || !$user->get($id)) {
            redirect(base_url('?c=User'));
        }
        
        $new_password = '';
        $confirm_password = '';
        if ($act == ACT_VALID_EDIT) {

      
            $new_password = isset($_POST['new_password']) ? $_POST['new_password'] : '';
            $confirm_password = isset($_POST['confirm_password']) ? $_POST['confirm_password'] : '';
            if ($user->validateChangePassword($new_password, $confirm_password)) {
                $user->pass = sha1($new_password);
                $user->update();
                redirect(base_url('?c=User'));
            }
        }

		$array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        
        $this->vars['new_password'] = $new_password;
        $this->vars['confirm_password'] = $confirm_password;
        $this->vars['user'] = $user;
        
        $this->vars['title'] = $title;

        $this->vars['array_menus'] = $array_menus;
        $this->vars['backlink'] = base_url('?c=User');
        $this->vars['section'] = $section;
        
        $this->display('change_password');
    }

    function delete($id = null) {
		
		$included_files = get_included_files();

        if (!in_array(APPLICATION_PATH.'/helpers/url_helper.php', $included_files))
                include APPLICATION_PATH.'/helpers/url_helper.php';
	
        $user = new Login();

        if ($id && !$user->get($id)) {
                redirect(base_url('?c=User'));
        }
        $user->delete();
        redirect(base_url('?c=User'));
    }
    
    function form() {
        
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
        
        $included_files = get_included_files();

        if (!in_array(APPLICATION_PATH.'/helpers/url_helper.php', $included_files))
                include APPLICATION_PATH.'/helpers/url_helper.php';
        
        if($_SESSION['is_controller'] != ADMIN_SUPPORT) {
            return redirect(base_url('?c=User'));
        }
        
        $section = 'user';
		
        if ($id) {
            $cfer = new Cfer(array(
            _LB_USER => 'user',
            _LB_EDIT_USER => base_url('c=User&a=form&id='.$id)));
        } else {
            $cfer = new Cfer(array(
            _LB_USER => 'user',
            _LB_ADD_USER => base_url('?c=User&a=form')));
        }
        
        $act = isset($_POST['act']) ? $_POST['act'] : false;
    
        $user = new Login();

        if ($id && !$user->get($id)) {
            redirect(base_url('?c=User'));
        }
        
        if ($act == ACT_VALID_EDIT) {
            
            $user->first_name = isset($_POST['first_name']) ? $_POST['first_name'] : null;
            $user->last_name = isset($_POST['last_name']) ? $_POST['last_name'] : null;
            $user->email = isset($_POST['email']) ? $_POST['email'] : null;
            $user->is_controller = isset($_POST['type']) ? $_POST['type'] : null;
            
            if ($id) {
                if (Login::emailExistByLoginID($user->email, $id))
                    MessageHandler::add(_MSG_EMAIL_EXISTS);
                else    
                    $user->update();
            }
            else {
                $user->pass = isset($_POST['password']) ? $_POST['password'] : null;
                $confirm_password = isset($_POST['confirm_password']) ? $_POST['confirm_password'] : null;
                
                if ($user->validateInput($confirm_password)) {
                    $user->pass = sha1($user->pass);
                    $user->insert();
                }
            }
            
            if (!MessageHandler::countError())
                redirect(base_url('?c=User'));
        }

        $array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        
        $title = _LB_USER;
        
        $this->vars['user'] = $user;
        $this->vars['title'] = $title;

        $this->vars['array_menus'] = $array_menus;
        $this->vars['backlink'] = base_url('?c=User');
        $this->vars['section'] = $section;
        
        $this->display('user_form');
    }
}
